//
//  XZIPAddressController.m
//  生活小助手
//
//  Created by lixiangzhou on 14-8-7.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "XZIPAddressController.h"
#import "XZBaseTool.h"
#import "XZIpParam.h"
#import "XZIpResult.h"
#import "XZIpLocation.h"
#import "XZFunctionKey.h"
#import "XZIpTool.h"
#import "MBProgressHUD+MJ.h"

#define kMarginLeft 20
#define kMarginTop 80
#define kIpLabelFont [UIFont systemFontOfSize:18]
#define kIpResultFont [UIFont systemFontOfSize:15]
#define kMarginY 10

@interface XZIPAddressController ()
@property (weak, nonatomic) UITextField *ipField;
@property (weak, nonatomic) UILabel *ipAddressLabel;
@property (weak, nonatomic) UIButton *confirmBtn;
@property (assign, nonatomic) BOOL netOK;
@end

@implementation XZIPAddressController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = kGlobalBackgroundColor;
    
    self.title = @"ip查询";
    
    [self addViews];
}

- (void)addViews
{
    UILabel *label = [[UILabel alloc] init];
    label.font = kIpLabelFont;
    label.text = @"查询的结果是：";
    CGSize size = [label.text sizeWithAttributes:@{NSFontAttributeName: kIpLabelFont}];
    label.width = size.width;
    label.height = size.height + 10;
    label.x = kMarginLeft;
    label.y = kMarginTop;
    [self.view addSubview:label];
    
    /* ------------------结果显示------------------*/
    UILabel *ipAddressLabel = [[UILabel alloc] init];
    ipAddressLabel.font = kIpResultFont;
//    ipAddressLabel.backgroundColor = [UIColor redColor];
    ipAddressLabel.numberOfLines = 0;
    ipAddressLabel.width = self.view.width - kMarginLeft * 2;
    ipAddressLabel.height = kIpResultFont.lineHeight * 3;
    ipAddressLabel.x = kMarginLeft;
    ipAddressLabel.y = CGRectGetMaxY(label.frame) + kMarginY;
    [self.view addSubview:ipAddressLabel];
    self.ipAddressLabel = ipAddressLabel;
    
    /* ------------------ip输入框------------------*/
    UITextField *ipField = [UITextField textFieldWithPlaceholder:@"输入ip地址或域名" leftMargin:5];
    ipField.width = self.ipAddressLabel.width;
    ipField.height = 35;
    ipField.x = kMarginLeft;
    ipField.y = CGRectGetMaxY(ipAddressLabel.frame) + kMarginY;
    [self.view addSubview:ipField];
    self.ipField = ipField;
    
    /* ------------------确定按钮------------------*/
    UIButton *confirmBtn = [[UIButton alloc] init];
    [confirmBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    confirmBtn.width = 100;
    confirmBtn.height = 30;
    confirmBtn.centerX = self.view.centerX;
    confirmBtn.y = CGRectGetMaxY(ipField.frame) + kMarginY;
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.view addSubview:confirmBtn];
    [confirmBtn addTarget:self action:@selector(getIpAddress) forControlEvents:UIControlEventTouchUpInside];
    self.confirmBtn = confirmBtn;
}

- (void)getIpAddress
{
    XZIpParam *param = [[XZIpParam alloc] init];
    param.key = [XZFunctionKey ipAddressKey];
    param.dtype = @"json";
    param.ip = [self.ipField.text lowercaseString];
    
    [MBProgressHUD showMessage:@"正在帮您查询"];
    
    [XZIpTool getIpWithParameter:param success:^(XZIpResult *responseObj) {
        self.netOK = YES;
        XZIpLocation *loc = responseObj.result;
        self.ipAddressLabel.text = [NSString stringWithFormat:@"地区：%@\n位置：%@", loc.area, loc.location];
        [MBProgressHUD hideHUD];
    } failure:^(NSError *error) {
        self.netOK = YES;
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:error.domain];

    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (!self.netOK) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"请检查你的网络"];
        }
        
    });
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
